package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//通过这个类，来处理/blog路径对应的请求
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf8");
        BlogDao blogDao = new BlogDao();
        //先尝试获取到req中的blogId参数，如果该参数存在，说明请求的是博客详情页
        //如果参数不存在，说明请求的是博客列表页
        String param = req.getParameter("blogId");
        if (param == null) {
            //从数据库中查询到博客列表，转成json格式，然后直接返回即可
            List<Blog> blogs = blogDao.selectAll();
            //把blogs转成json格式
            String respJson = objectMapper.writeValueAsString(blogs);
            resp.getWriter().write(respJson);
        } else {
            //参数存在，获取到博客详情
            int blogId = Integer.parseInt(param);
            Blog blog = blogDao.selectOne(blogId);
            String respJson = objectMapper.writeValueAsString(blog);
            resp.getWriter().write(respJson);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            //当前用户未登录，不能提交博客
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("提交博客失败！当前用户未登录");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //当前用户未登录，不能提交博客
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("提交博客失败！当前用户未登录");
            return;
        }
        //先指定好解析请求的格式
        req.setCharacterEncoding("utf8");
        //先从请求中，获取到博客的标题和正文
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if (title == null || "".equals(title) || content == null || "".equals(content)) {
            //请求参数不对，不能提交博客
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("提交博客失败！请求参数不对");
            return;
        }
        //构造blog对象，把当前的信息填进去，并插入到数据库中
        //此处要给blog设置的属性主要是标题和正文，以及用户id（作者信息）
        //blogId和posttime不需要手动指定
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        BlogDao blogDao = new BlogDao();
        blogDao.insert(blog);
        //博客插入数据库成功后，重定向到博客列表页
        resp.sendRedirect("blog_list.html");
    }
}
